const app = getApp();
const util = require('../../../utils/util');
const requestUtil = require('../../../utils/request');
const api = require('../../../config/api');

Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    cardInfo: null,
    cardList: null,
    cardListIndex: 0,
    cardListTotal: 0, // 列表总数
    companyList: [], // 公司列表
    companyListIndex: 0, // 公司列表索引
  },

  onShow() {
    let userType = app.globalData.userType;
    let cardInfo = userType == 1 ? app.globalData.employeeInfo : userType == 2 ? app.globalData.customInfo : null;
    if (userType == 1) {
      this.setData({
        cardInfo: cardInfo,
        userType: userType,
      })
      let params = {
        companyId: this.data.cardInfo.companyId
      };
      this.getCardList(params);
    } else if (userType == 2) {
      this.setData({
        userType: userType
      })
      this.getCompanyList();
    }
  },

  /**
   * 获取页面列表 
   */
  getCardList: async function (params) {
    let res = await requestUtil.request("POST", api.urls.getCompanyUserList, params)
    if (res.data.code != 0) {
      util.showToast(res.data.msg);
      return;
    }
    for (let list of res.data.data) {
      if (list.headImgUrl == null) {
        continue;
      }
      let url = list.headImgUrl;
      list.headImgUrl = api.picUrl + url;
    }
    this.setData({
      cardList: res.data.data,
      cardListTotal: res.data.data.length,
    })
  },

  /**
   * 点击按钮查询
   */
  searchItems: function (e) {
    let value = e.detail.value;
    let params = {
      name: value
    };
    this.getCardList(params);
  },

  /**
   * 打电话
   */
  onCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },

  /**
   * 跳转到用户名片页面
   */
  toUserCardDetail: function (e) {
    // 固定这个页面获取到的都是员工名片吧
    let userType = 1;
    wx.navigateTo({
      url: '../../card/card?id=' + e.currentTarget.dataset.id + '&type=' + userType,
    })
  },

  /**
   * 公司列表选择
   */
  companyPickerChange: function (e) {
    this.setData({
      companyListIndex: e.detail.value
    })
    let companyId = this.data.companyList[this.data.companyListIndex].id;
    app.globalData.companyId = companyId;
    let params = {
      companyId: companyId,
    };
    this.getCardList(params);
  },

  /**
   * 获取公司列表
   */
  getCompanyList: async function () {
    let res = await requestUtil.request("POST", api.urls.getCompanyList, null);
    if (res.data.code != 0) {
      util.showToast(res.data.msg);
      return;
    }
    let data = res.data.data;
    for(let i in data) {
        if(data[i].appid == api.appid) {
            this.setData({
                companyList: data,
                companyListIndex: i,
            })
            let params = {
                companyId: data[i].id
            };
            this.getCardList(params);
            break;
        }
    }
    
  },

  /**
   * 跳转到聊天界面
   */
  onChat: function (e) {
    wx.navigateTo({
      url: '../loginpage/login?id=' + e.currentTarget.dataset.id + '&userType=2'
    })
  }
});